package com.winhxd.b2c.admin.module.order.controller;

import com.winhxd.b2c.admin.common.security.annotation.CheckPermission;
import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.context.AdminUser;
import com.winhxd.b2c.common.context.UserContext;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.*;
import com.winhxd.b2c.common.domain.order.vo.*;
import com.winhxd.b2c.common.domain.system.security.enums.PermissionEnum;
import com.winhxd.b2c.common.feign.order.OrderManagementServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(value = "后台小程序订单管理", tags = "后台小程序订单管理")
@RequestMapping("/order")
@CheckPermission(PermissionEnum.ORDER_MANAGEMENT_LIST)
public class OrderController {

    private static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Resource
    private OrderManagementServiceClient orderManagementServiceClient;


    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_LIST)
    @ApiOperation(value = "订单列表", notes = "订单列表")
    @PostMapping(value = "/findOrderManagementList")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<PagedList<OrderManagementListVO>> findOrderManagementList(@RequestBody OrderManagementListCondition condition) {
        ResponseResult<PagedList<OrderManagementListVO>> responseResult = orderManagementServiceClient.findOrderManagementList(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_SHOPPING_LIST)
    @ApiOperation(value = "购物清单", notes = "购物清单")
    @PostMapping(value = "/findShoppingList")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<OrderShoppingListVO> findShoppingList(@RequestBody OrderShoppingListCondition condition) {
        ResponseResult<OrderShoppingListVO> responseResult = orderManagementServiceClient.findShoppingList(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_DISTRIBUTION_LIST)
    @ApiOperation(value = "配货清单", notes = "配货清单")
    @PostMapping(value = "/findDistributionList")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<OrderDistributionListVO> findDistributionList(@RequestBody OrderDistributionListCondition condition) {
        ResponseResult<OrderDistributionListVO> responseResult = orderManagementServiceClient.findDistributionList(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_ORDER_DETAILS)
    @ApiOperation(value = "订单详情", notes = "订单详情")
    @PostMapping(value = "/findOrderDetails")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<OrderManagementDetailsVO> findOrderDetails(@RequestBody OrderDetailsCondition condition) {
        ResponseResult<OrderManagementDetailsVO> responseResult = orderManagementServiceClient.findOrderDetails(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_DELIVER_GOODS)
    @ApiOperation(value = "发货页面", notes = "发货页面")
    @PostMapping(value = "/getDeliverGoods")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<OrderDeliverGoodsInfoVO> getDeliverGoodsInfo(@RequestBody OrderDeliverGoodsInfoCondition condition) {
        ResponseResult<OrderDeliverGoodsInfoVO> responseResult = orderManagementServiceClient.getDeliverGoodsInfo(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_DELIVER_GOODS)
    @ApiOperation(value = "发货", notes = "发货")
    @PostMapping(value = "/saveDeliverGoods")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<Integer> saveDeliverGoods(@RequestBody OrderDeliverGoodsInfoCondition condition) {
        ResponseResult<Integer> responseResult = orderManagementServiceClient.saveDeliverGoods(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_ORDER_STATUS_TO_CLOSE)
    @ApiOperation(value = "关闭订单", notes = "关闭订单")
    @PostMapping(value = "/modifyOrderStatusToClose")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<Integer> modifyOrderStatusToClose(@RequestBody OrderStatusToCloseCondition condition) {
        //获取当前后台管理员用户信息
        AdminUser adminUser = UserContext.getCurrentAdminUser();
        condition.setUserName(adminUser.getUsername());
        ResponseResult<Integer> responseResult = orderManagementServiceClient.modifyOrderStatusToClose(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_COLLECT_REFUND_RECORD)
    @ApiOperation(value = "收退款记录", notes = "收退款记录")
    @PostMapping(value = "/findCollectAndRefundRecord")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<OrderCollectAndRefundRecordVO> findCollectAndRefundRecord(@RequestBody OrderCollectAndRefundRecordCondition condition) {
        ResponseResult<OrderCollectAndRefundRecordVO> responseResult = orderManagementServiceClient.findCollectAndRefundRecord(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_RECEIVING_DELIVERY_RECORD)
    @ApiOperation(value = "收发货记录", notes = "收发货记录")
    @PostMapping(value = "/findReceivingAndDeliveryRecord")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<OrderReceivingAndDeliveryRecordVO> findReceivingAndDeliveryRecord(@RequestBody OrderReceivingAndDeliveryRecordCondition condition) {
        ResponseResult<OrderReceivingAndDeliveryRecordVO> responseResult = orderManagementServiceClient.findReceivingAndDeliveryRecord(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_REBATE_INFORMATION)
    @ApiOperation(value = "返利信息", notes = "返利信息")
    @PostMapping(value = "/findRebateInformation")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<List<OrderRebateInformationVO>> findRebateInformation(@RequestBody OrderRebateInformationCondition condition) {
        //获取当前后台管理员用户信息
        AdminUser adminUser = UserContext.getCurrentAdminUser();
        condition.setCustomerId(adminUser.getCustomerId());
        ResponseResult<List<OrderRebateInformationVO>> responseResult = orderManagementServiceClient.findRebateInformation(condition);
        return responseResult;
    }

    @CheckPermission(PermissionEnum.ORDER_MANAGEMENT_ORDER_LOG)
    @ApiOperation(value = "订单日志", notes = "订单日志")
    @PostMapping(value = "/findOrderLog")
    @ApiResponses({@ApiResponse(code = BusinessCode.CODE_OK, message = "操作成功"), @ApiResponse(code = BusinessCode.CODE_1001, message = "服务器内部错误")})
    public ResponseResult<OrderLogVO> findOrderLog(@RequestBody OrderLogCondition condition) {
        ResponseResult<OrderLogVO> responseResult = orderManagementServiceClient.findOrderLog(condition);
        return responseResult;
    }


}
